<?php
/**
 * Test the apache password/group file classes.
 * 
 * @package AP5L
 * @subpackage Apache
 * @license http://www.gnu.org/licenses/gpl-3.0.html GPLv3
 * @copyright 2007, Alan Langford
 * @version $Id: ApacheUserAuthUnitTest.php 91 2009-08-21 02:45:29Z alan.langford@abivia.com $
 * @author Alan Langford <alan.langford@abivia.com>
 * 
 * @todo Update to PHP5
 * @todo Move to PHPUnit
 */

require_once('../ApacheUserAuth.php');

function dumpFile($fid) {
    $data = file_get_contents($fid);
    echo $fid . ':<br><pre>' . $data . '</pre>';
}

//
// Write a trivial (and invalid) apache format user and group file
//
$tests = 0;
$fails = 0;
$groupFid = 'testgroup';
$passFid = 'testpass';

$fh = fopen($passFid, 'w');
fwrite($fh, 'user1:{plain}foo' . chr(10));
fwrite($fh, 'user2:{plain}bar' . chr(10));
fwrite($fh, 'user3:{plain}foobar' . chr(10));
fclose($fh);
$fh = fopen($groupFid, 'w');
fwrite($fh, 'grp1: user1' . chr(10));
fwrite($fh, 'grp2: user1 user2' . chr(10));
fwrite($fh, 'grp3: user1 user2 user3' . chr(10));
fclose($fh);

$auth = new ApacheUserAuth();
$auth -> setPasswordFile('testpass');
$auth -> setGroupFile('testgroup');

echo 'Initial state<br>';
dumpfile($passFid);
dumpFile($groupFid);

$auth -> loadUsers();
$auth -> loadGroups();
echo '<pre>';
print_r($auth -> userList);
echo '</pre>';

echo 'Password set tests (make password file valid)<br/>';
if (! $auth -> setApachePassword('user1', 'foo')) {
    $fails++;
    echo 'FAIL: ' . $auth -> errMsg . '<br>';
}
$tests++;

if (! $auth -> setApachePassword('user2', 'bar')) {
    $fails++;
    echo 'FAIL: ' . $auth -> errMsg . '<br>';
}
$tests++;

if (! $auth -> setApachePassword('user3', 'foobar')) {
    $fails++;
    echo 'FAIL: ' . $auth -> errMsg . '<br>';
}
$tests++;

if ($auth -> setApachePassword('nonexistentuser', 'fail')) {
    $fails++;
    echo 'FAIL: No error setting password for bad user<br>';
}
$tests++;
if ($auth -> result != 4) {
    $fails++;
    echo 'FAIL: Got error ' . $auth -> result . ' setting password for bad user, expected 4<br>';
}
$tests++;

echo 'Add user<br/>';
if (! $auth -> addUser('newuser', 'newpass')) {
    $fails++;
    echo 'FAIL: ' . $auth -> errMsg . '<br/>';
}
$tests++;

echo 'Add group<br/>';
if (! $auth -> addGroup('newgroup', 'newuser')) {
    $fails++;
    echo 'FAIL: ' . $auth -> errMsg . '<br/>';
}
$tests++;

echo 'Delete group<br/>';
if (! $auth -> deleteGroup('grp1')) {
    $fails++;
    echo 'FAIL: ' . $auth -> errMsg . '<br/>';
}
$tests++;

echo 'Add bad user to group<br/>';
if ($auth -> addUserToGroup('newgroup', 'nogood')) {
    $fails++;
    echo 'FAIL: No error.<br/>';
}
$tests++;

echo 'Add users to group<br/>';
if (! $auth -> addUserToGroup('newgroup', array('user1', 'user2', 'user3', 'newuser'))) {
    $fails++;
    echo 'FAIL: ' . $auth -> errMsg . '<br/>';
}
$tests++;

echo 'Delete user from group<br/>';
if (! $auth -> deleteUserFromGroup('newgroup', 'user1')) {
    $fails++;
    echo 'FAIL: ' . $auth -> errMsg . '<br/>';
}
$tests++;

echo 'Delete user<br/>';
if (! $auth -> deleteUser('user1')) {
    $fails++;
    echo 'FAIL: ' . $auth -> errMsg . '<br/>';
}
$tests++;

echo 'Add user to group (cleanup test)<br/>';
if (! $auth -> addUserToGroup('grp3', 'newuser')) {
    $fails++;
    echo 'FAIL: ' . $auth -> errMsg . '<br/>';
}
$tests++;

echo 'Summary: ' . $tests . ' tests, ' . $fails . ' failures.<br>';
echo 'Final Files<br>';
dumpfile($passFid);
dumpFile($groupFid);

?>
